package com.example.bicycle.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import com.example.bicycle.domain.mqtt.PublishSettingMessageVo;
import com.example.bicycle.mqtt.MqttMessageService;
import com.example.bicycle.service.ElectricBikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@SaCheckLogin
@RestController
@RequestMapping("/publish/")
public class PublishController {
    private final ElectricBikeService  electricBikeService;
    private final MqttMessageService mqttMessageService;
    private final String topic = "$delayed/1/control-";


    @Autowired
    public PublishController(ElectricBikeService electricBikeService, MqttMessageService mqttMessageService) {
        this.electricBikeService = electricBikeService;
        this.mqttMessageService = mqttMessageService;
    }

    @PostMapping("setting")
    public SaResult setting(@RequestParam("bikeId") Integer bikeId, @RequestParam("lightThreshold") Float lightThreshold,@RequestParam("isSyncTime") Boolean isSyncTime) {
        /* 发送数据给MQTT服务器 */

        PublishSettingMessageVo setting = electricBikeService.setting(bikeId, lightThreshold, isSyncTime);
        mqttMessageService.publish(topic+bikeId.toString(),setting);
        return SaResult.ok("设置成功");

    }
}
